package com.i2bt.app.friend.model;

import com.i2bt.common.AndroidRestContract;

import com.i2bt.app.BaseFragment;
import com.i2bt.app.BaseJsonModel;
import com.i2bt.app.friend.FriendFragment;
import com.i2bt.bean.FriendBean;
import com.i2bt.bean.FriendInfoBean;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * 
 * Title : 好友json处理类
 *
 * Description : 打包和解包
 *
 * CreatedTime : 2014-8-18
 *
 * @author wuliao & kakaxi & yi
 *
 * @version 1.0
 *
 */

public class FriendJsonHandle extends BaseJsonModel {

	public FriendJsonHandle(BaseFragment fragment) {
		super(fragment);
	}

	@Override
	protected String logicToWrap() throws JSONException {
		
		switch (beanParam.getAct()) {
		
		case AndroidRestContract.SHOW_FRIEND : {
			jsonStrParam = wrapShowFriend();
			break;
		}
		
		case AndroidRestContract.DELETE_FRIEND :  {
			jsonStrParam = wrapDeleteFriend();
			break;
		}
		
		}
		
		return jsonStrParam;
	}
	
	/**
	 * 打包获取好友列表的json
	 * @return
	 * @throws JSONException
	 */
	private String wrapShowFriend() throws JSONException {
		
		wrapCommonJson();
		
		return jsonParam.toString();
	}
	
	private String wrapDeleteFriend() throws JSONException {
		
		wrapCommonJson();
		
		FriendBean friendBean = (FriendBean) beanParam;
		jsonParam.put(AndroidRestContract.FRIEND_ID, friendBean.getFriendId());
		
		return jsonParam.toString();
	}

	@Override
	protected void logicToUnwrap(boolean isSuc, String result)
			throws JSONException {
		
		if (isSuc) {
			jsonParam = new JSONObject(result);

			if (jsonParam.getBoolean(AndroidRestContract.SUCCEED)) {
				FriendFragment friendFragment = (FriendFragment) fragment;

				switch  (jsonParam.getInt(AndroidRestContract.ACTION)) {

				//显示好友列表的响应处理
				case AndroidRestContract.SHOW_FRIEND : {
					friendFragment.showFriendResponse(unwrapShowFriend());
					break;
				}
				
				//取消关注
				case AndroidRestContract.DELETE_FRIEND : {
					friendFragment.deleteFriendResponse();
					break;
				}

				}
			}
			else {
				fragment.toastHandler(jsonParam.
						getString(AndroidRestContract.MESSAGE));
			}
		}
		else {
			fragment.toastHandler(result);
		}
		
	}
	
	/**
	 * 解包显示好友的json
	 * @return
	 * @throws JSONException
	 */
	private List<FriendInfoBean> unwrapShowFriend() throws JSONException {
		
		List<FriendInfoBean> friendInfoList = new ArrayList<FriendInfoBean>();
		JSONArray jsonArrayTemp = jsonParam.getJSONArray(
				AndroidRestContract.FRIEND_LIST);
		for (int i = 0; i < jsonArrayTemp.length(); i++) {
			friendInfoList.add( wrapFriendBean(jsonArrayTemp.getJSONArray(i)) );
		}
		
		return friendInfoList;
	}
	
	/**
	 * 把解包的好友放入到好友信息类里面
	 * @param jsonArray
	 * @return
	 * @throws JSONException
	 */
	private FriendInfoBean wrapFriendBean(JSONArray jsonArray) throws JSONException {
		FriendInfoBean infoBean = new FriendInfoBean();
		infoBean.setFriendId(jsonArray.getString(0));
		infoBean.setFriendName(jsonArray.getString(1));
		return infoBean;
	}

}
